package com.shujia.student;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;

public class Reducer {
    public static void main(String[] args) throws Exception {

        /**
         * 合并多个结果
         *
         */


        HashMap<String, Long> map = new HashMap<>();

        File pfile = new File("java/data/tmp2");

        File[] files = pfile.listFiles();
        for (File file : files) {
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] split = line.split(",");
                String clazz = split[0];
                long num = Long.parseLong(split[1]);

                Long aLong = map.get(clazz);
                if (aLong != null) {
                    map.put(clazz, num + aLong);
                } else {
                    map.put(clazz, num);
                }
            }
        }

        System.out.println(map);


    }
}
